<template>
    <div class="app-container">
        <div class="head">
            <img src="@/assets/img/Layout/position.png" />
            <p class="see-block">领料单详情</p>
        </div>
        <div class="view-container">
            <el-row class="status-title">
                <el-col class="mr20 width100" v-if="detailData.approveStatus == '0' && detailData.status == '0'">
                    <el-button
                        class="btn-default width100"
                        type="primary"
                        @click="audit()"
                        v-hasPermi="['system:materialRequisition:check']"
                    >审核</el-button>
                </el-col>
                <el-col class="mr20 width100" v-if="detailData.approveStatus == '2' && detailData.status == '0'">
                    <el-button
                        class="btn-default width100"
                        type="primary"
                        @click="edit()"
                        v-hasPermi="['system:materialRequisition:edit']"
                    >编辑</el-button>
                </el-col>
                <el-col class="mr20 width100" v-if="detailData.approveStatus == '1' && detailData.status == '1'">
                    <el-button
                        class="btn-default width100"
                        type="primary"
                        @click="confirm()"
                    >确定</el-button>
                </el-col>
                <el-col :span="10" class="status-container">
                    <div class="status-item">
                        <span>草稿</span>
                        <span :class="{ 'active' : detailData.status == '0'}">待审核</span>
                        <span :class="{ 'active' : detailData.status == '1'}">待领用</span>
                        <span :class="{ 'active' : detailData.status == '2'}">已领用</span>
                    </div>
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="9">
                    <span>单据编码：</span>{{ detailData.code ?  detailData.code : '暂无'}}
                </el-col>
                <el-col :span="9">
                    <span>业务日期：</span>{{ detailData.businessDate ?  detailData.businessDate : '暂无'}}
                </el-col>
            </el-row>
            <el-row class="detail-item">
                <el-col :span="9">
                    <span>业务部门：</span>{{ detailData.businessUnit ?  detailData.businessUnit : '暂无'}}
                </el-col>
                <el-col :span="9">
                    <span>业务类型：</span>{{ detailData.businessType == '0' ? '生产领料' : detailData.businessType == '1' ? '赠品领用' : detailData.businessType == '2' ? '质检领用' : '暂无'}}
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>接收仓库：</span>{{ detailData.receivingWarehouse ?  detailData.receivingWarehouse : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item" v-if="detailData.planFinishCode !== '' && detailData.planFinishCode !== null">
                        <el-col :span="24">
                            <span>完工入库单：</span>{{ detailData.planFinishCode }}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>创建人：</span>{{ detailData.createBy ?  detailData.createBy : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>创建时间：</span>{{ detailData.createTime ?  detailData.createTime : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>领料人：</span>{{ detailData.picker ?  detailData.picker : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>审核人：</span>{{ detailData.approveName ?  detailData.approveName : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>审核时间：</span>{{ detailData.approveTime ?  detailData.approveTime : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>状态：</span>
                            <font v-if="detailData.status == 0" style="color:#13C2C2">待审核</font>
                            <font v-if="detailData.status == 1" style="color:#13C2C2">待领用</font>
                            <font v-if="detailData.status == 2" style="color:#52C41A">已领用</font>
                        </el-col>
                    </div>
                </el-col>
                <el-col :span="9">
                    <div class="detail-item">
                        <el-col :span="24">
                            <span>备注：</span>{{ detailData.remark ?  detailData.remark : '暂无'}}
                        </el-col>
                    </div>
                </el-col>
            </el-row>
            <el-row class="mt20 mb10" v-if="detailData.status !== '2' && this.scanType == '1'">
                <el-col :span="24">
                    <el-button class="btn-scanCode width100" :class="{'btn-scanCodeDel' : isTrue}" type="primary" @click="delScanCode">删除扫码</el-button>
                </el-col>
            </el-row>
            <el-row class="mt20">
                <el-col :span="24">
                    <el-table :data="detailData.details" @row-click="scanCode" border>
                        <el-table-column label="序号" type="index" align="center" width="50" />
                        <el-table-column :label="item.label" :prop="item.prop" :width="item.width" v-for="(item, index) in columns" :key="index" align="center">
                            <template slot-scope="scope">
                                <template v-if="detailData.approveStatus == '1' && detailData.status == '1'">
                                    <!-- 完工入库单 -->
                                    <template v-if="detailData.businessType == '2'">
                                        <template v-if="item.type == 'realNum'">
                                            <el-input placeholder="扫码领料" v-model="scope.row[item.prop]" disabled />
                                        </template>
                                        <template v-if="item.type == '1'">
                                            {{ scope.row[item.prop] }}
                                        </template>
                                    </template>
                                    <!--  -->
                                    <template v-else>
                                        <template v-if="item.type == 'realNum'">
                                            <template v-if="scope.row.isSerial == '0' || detailData.businessType == '2'">
                                                <el-input placeholder="扫码领料" v-model="scope.row[item.prop]" disabled />
                                            </template>
                                            <template v-else>
                                                <el-input placeholder="请输入数量" v-model="scope.row[item.prop]" @click.native.stop="stopScanCode" @input="(val) => inputNum(val, scope.row)" @blur="onInventory(scope.row)" :disabled="scope.row.positionList ? true : false" />
                                            </template>
                                        </template>
                                        <template v-if="item.type == 'warehouse'">
                                            <el-input v-model="scope.row[item.prop]" placeholder="扫码领料" disabled v-if="scope.row.isSerial == '0'" />
                                            <el-select v-else v-model="scope.row[item.prop]" style="width:100%;" placeholder="请选择" @change="val => onSelectWarehouse(val, scope.row)" :disabled="scope.row.positionList ? true : false">
                                                <el-option
                                                    v-for="item in warehouseList"
                                                    :key="item.warehouseId"
                                                    :label="item.name"
                                                    :value="item.warehouseId">
                                                </el-option>
                                            </el-select>
                                        </template>
                                        <template v-if="item.type == 'position'">
                                            <el-input v-model="scope.row[item.prop]" placeholder="扫码领料" disabled v-if="scope.row.isSerial == '0'" />
                                            <el-input v-else v-model="scope.row[item.prop]" disabled placeholder="点击按钮搜索" @click.native.stop="stopScanCode">
                                                <el-button slot="append" icon="el-icon-search" @click.stop="onClickWarehouseList(scope.row, scope.$index)"></el-button>
                                            </el-input>
                                        </template>
                                        <template v-if="item.type == 'more'">
                                            <template v-if="scope.row.isSerial == '0'">
                                                <template v-if="scope.row.positionList !== null">
                                                    <el-tooltip effect="dark" content="多仓位" placement="bottom" >
                                                        <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.row, 'isSerial')"></span>
                                                    </el-tooltip>
                                                </template>
                                            </template>
                                            <template v-else>
                                                <el-tooltip effect="dark" content="多仓位" placement="bottom" >
                                                    <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.row)"></span>
                                                </el-tooltip>
                                            </template>
                                        </template>
                                        <template v-if="item.type == '1'">
                                            {{ scope.row[item.prop] }}
                                        </template>
                                    </template>
                                </template>
                                <template v-else>
                                    <template v-if="detailData.approveStatus == '1' && detailData.status == '2'">
                                        <template v-if="item.type == 'more'">
                                            <template v-if="scope.row.isSerial == '0'">
                                                <template v-if="scope.row.positionList !== null">
                                                    <el-tooltip effect="dark" content="多仓位" placement="bottom" >
                                                        <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.row, 'isSerial')"></span>
                                                    </el-tooltip>
                                                </template>
                                            </template>
                                            <template v-else>
                                                <el-tooltip effect="dark" content="多仓位" placement="bottom" >
                                                    <span class="el-icon-s-grid more" @click.stop="showMorePosition(scope.row)"></span>
                                                </el-tooltip>
                                            </template>
                                        </template>
                                    </template>
                                    <span>{{ scope.row[item.prop] }}</span>
                                </template>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>
        </div>
        <!-- 审核 -->
        <el-dialog :visible.sync="open" width="500" top="100px" append-to-body title="审核意见" center>
            <el-row>
                <el-col :span="24">
                    <textarea
                        class="approve-textarea"
                        v-model="approveRemark"
                        type="textarea"
                        placeholder="请输入意见和看法"
                    />
                </el-col>
            </el-row>
            <div slot="footer" class="dialog-footer">
                <el-button
                    type="primary"
                    class="btn-success mr20 width100"
                    @click="agreeApprove()"
                >同意</el-button>
                <el-button
                    type="primary"
                    class="btn-cancel width100"
                    @click="disagreeApprove()"
                >拒绝</el-button>
            </div>
        </el-dialog>
        <!-- 多仓位 -->
        <el-dialog :visible.sync="morePosition" width="800px" top="100px" append-to-body center :close-on-click-modal='false' :close-on-press-escape='false' :show-close="false">
            <el-table :data="moreData" @row-click="scanCodeMorePosition">
                <el-table-column label="名称" align="center" prop="productName" width="150" />
                <el-table-column label="实际数量" align="center" prop="endNum" width="100">
                    <template slot-scope="scope">
                        <template v-if="scope.row.isSerial == '0'">
                            <el-input placeholder="扫码领料" v-model="scope.row.endNum" disabled />
                        </template>
                        <template v-else>
                            <template v-if="detailData.status == '1'">
                                <el-input placeholder="请输入" v-model="scope.row.endNum" @input="(val) => inputEditNum(val, scope.row)" @blur="(val) => onBlurEditNum(val, scope.row)" />
                            </template>
                            <template v-else>
                                {{ scope.row.endNum }}
                            </template>
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="仓库" align="center" prop="warehouse">
                    <template slot-scope="scope">
                        <el-select v-model="scope.row.warehouse" disabled>
                            <el-option
                                v-for="item in warehouseList"
                                :key="item.warehouseId"
                                :label="item.name"
                                :value="item.warehouseId">
                            </el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column label="仓位" align="center" prop="positionName">
                    <template slot-scope="scope">
                        <el-input v-if="scope.row.isSerial == '0'" v-model="scope.row.positionName" disabled />
                        <template v-else>
                            <el-input v-if="detailData.status == '1'" v-model="scope.row.positionName" disabled placeholder="点击按钮搜索">
                                <el-button slot="append" icon="el-icon-search" @click="morePositionWarehouseList(scope.row, scope.$index)"></el-button>
                            </el-input>
                            <el-input v-else v-model="scope.row.positionName" disabled />
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="" align="center" width="90px" class-name="small-padding fixed-width">
                    <template slot-scope="scope" v-if="scope.row.isSerial !== '0' && detailData.status == '1'">
                        <span class="el-icon-circle-plus-outline icon-more-position" @click="onAddMorePosition(scope.row)"></span>
                        <span class="el-icon-circle-close icon-more-position" @click="onDeleteMorePosition(scope.row)" v-if="scope.$index !== 0"></span>
                    </template>
                </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" class="btn-success width100" @click="onHideMorePosition">关闭</el-button>
            </div>
        </el-dialog>
        <!-- 扫码框 -->
        <!-- class="input-data" -->
        <input v-model="scanCodeData.code"
            class="input-data"
            ref="getFocus"
            type="password"
            @keydown.enter="numberEnterDown()"
            @keyup.enter="numberEnterUp()"
        />
    </div>
</template>

<script>
import { materialRequisitionEditSingle, materialRequisitionDetail, materialRequisitionCheck, morePositionList, morePositionAdd, morePositionEdit, morePositionUpdate, morePositionAddSingle, morePositionDelSingle, materialRequisitionScanCode, materialRequisitionScanCodeDel, confirmPicking } from "@/api/system/warehouse/materialRequisition"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"
import { orderSearchStorage } from "@/api/system/order/order"
export default {
    name: "detail",
    data() {
        return {
            id: '',
            detailData: '',
            warehouseList: [], // 仓库列表
            // 表头
            columns: [{
                prop: "productCode",
                label: "物料编码",
                width: 150,
                type: '1'
            },{
                prop: "productName",
                label: "物料名称",
                type: '1',
                width: 150,
            },{
                prop: "productSpace",
                label: "物料规格",
                type: '1',
                width: 150,
            },{
                prop: "planNum",
                label: "领用数量",
                type: '1'
            },
            // },{
            //     prop: "batchNumber",
            //     label: "批次号",
            //     width: 100,
            //     type: '1'
            // },{
            {
                prop: "realNum",
                label: "实际数量",
                width: 120,
                type: 'realNum'
            },{
                prop: "unit",
                label: "单位",
                type: '1'
            },{
                prop: "warehouse",
                label: "仓库",
                width: 130,
                type: 'warehouse'
            },{
                prop: "positionName",
                label: "仓位",
                width: 200,
                type: 'position'
            },{
                prop: "remark",
                label: "备注",
                type: '1'
            },{
                label: "",
                width: 60,
                type: 'more'
            }],
            open: false,
            approveRemark: '',
            morePosition: false,
            moreData: [],
            scanCodeData: {
                code: ''
            },
            isTrue: false,
            scanCodeType: '',
            scanType: ''
        }
    },
    created(){
        if(this.$route.query.data && this.$route.query.data !== '[object Object]'){
            // 仓位数据
            this.id = this.$route.query.id
            this.detailData = this.$route.query.data
            let index = this.$route.query.index
            this.getMaterialRequisitionEdit(this.detailData, index)
            this.$router.push({ query: {id: this.$route.query.id } })
        }else{
            this.id = this.$route.query.id
            this.getDetailData()
        }
        // 多仓位仓位数据
        if(this.$route.query.list && this.$route.query.list !== '[object Object]'){
            let list = this.$route.query.list
            let index = this.$route.query.index
            morePositionList(list.businessId).then(res => {
                if(res.code == 200){
                    this.morePosition = true
                    this.moreData = res.rows
                    this.moreData[index] = list
                    morePositionUpdate(this.moreData[index]).then(res => {
                        if(res.code == 200){
                            this.getDetailData() 
                            this.$router.push({ query: {id: this.id} })
                        }else{
                            this.$modal.msgError(res.msg)
                            morePositionList(list.businessId).then(res => {
                                if(res.code == 200){
                                    res.rows.forEach(item => {
                                        if(item.endNum == 0){
                                        return item.endNum = ''
                                        }
                                    })
                                    this.moreData = res.rows
                                    this.$router.push({ query: {id: this.id} })
                                }
                            })
                        }
                    })
                }
            })
        }
    },
    methods: {
        // 获取详情数据
        getDetailData(){
            materialRequisitionDetail(this.id).then(res => {
                this.detailData = res.data
                if(this.detailData.approveStatus == '1' && this.detailData.status == '1'){
                    res.data.details.forEach(item => {
                        if(item.realNum == 0 || item.realNum == null){
                            item.realNum = ''
                        }
                    })
                }
                let isSerial = res.data.details.some(val => val.isSerial == '0')
                if(isSerial == true){
                    this.scanType = '1'
                }
                this.getWarehouseList()
            })
        },
        // 获取仓库列表
        getWarehouseList(){
            queryWarehouse().then(res => {
                if(res.code == 200){
                    this.warehouseList = res.data
                    // 接收仓库id转换
                    let arr = res.data.find(e => {
                        return e.warehouseId == this.detailData.receivingWarehouse
                    })
                    if(arr != undefined){
                        this.detailData.receivingWarehouse = arr.name
                    }
                    // 产品仓库id转换
                    this.detailData.details.forEach(i => {
                        // console.log(i)
                        if(i.warehouse !== null || i.warehouse !== ''){
                            let dataList = res.data.find(item => {
                                return item.warehouseId == i.warehouse
                            })
                            if(dataList != undefined){
                                i.warehouse = dataList.name
                            }
                        }
                    })
                }
            })
        },
        // 审核
        audit(){
            this.open = true
        },
        // 通过
        agreeApprove(){
            materialRequisitionCheck(this.id, '1', '1', this.approveRemark).then(res => {
                if(res.code == 200){
                    this.getDetailData()
                    this.open = false
                    this.$modal.msgSuccess("操作成功")
                    this.$tab.closeOpenPage({ path: "/material/materialRequisition/list" })
                }
            })
        },
        // 拒绝
        disagreeApprove(){
            materialRequisitionCheck(this.id, '0', '2', this.approveRemark).then(res => {
                if(res.code == 200){
                    this.getDetailData()
                    this.open = false
                    this.$modal.msgSuccess("操作成功")
                    this.$tab.closeOpenPage({ path: "/material/materialRequisition/list" })
                }
            })
        },
        // 编辑
        edit(){
            this.$tab.closeOpenPage({ path: "/material/materialRequisition/edit", query:{id: this.id} })
        },
        // 产品仓库选择
        onSelectWarehouse(val, row){
            materialRequisitionEditSingle({id: row.id, warehouse: row.warehouse, updateWarehouse: 1 }).then(res => {
                if(res.code == 200){
                    this.getDetailData()
                }
            })
        },
        // 数据编辑
        getMaterialRequisitionEdit(item, index){
            materialRequisitionEditSingle(item.details[index]).then(res => {
                if(res.code == 200){
                    this.getDetailData()
                }
            })
        },
        // 阻止事件
        stopScanCode(){},
        // 扫码领料
        scanCode(row){
            this.isTrue = false
            this.scanCodeType = '0'
            if(this.detailData.status !== '2' && this.detailData.status !== '0'){
                if(this.detailData.businessType == '2'){
                    this.getScanCode(row)
                }else{
                    // 判断是否启用扫码
                    if(row.isSerial == '0'){
                        this.getScanCode(row)
                    }else{
                        this.scanCodeData = {}
                        // this.$modal.msgError('未启用扫码!')
                    }
                }
            }
        },
        getScanCode(row){
            this.$refs.getFocus.focus()
            this.scanCodeData = {
                materialRequisitionId: this.id,
                materialRequisitionDetailId: row.id,
            }
            this.$modal.msgSuccess("开始扫码！")
        },
        // 删除扫码
        delScanCode(){
            if(this.detailData.status !== '2' && this.detailData.status !== '0'){
                this.isTrue = true
                this.scanCodeType = '1'
                this.$refs.getFocus.focus()
                this.scanCodeData = {
                    materialRequisitionId: this.id,
                }
                this.$modal.msgSuccess("删除扫码！")
            }
        },
        // 多仓位扫码领料
        scanCodeMorePosition(row){
            // console.log(row)
            if(row.isSerial == '0'){
                this.$refs.getFocus.focus()
                this.scanCodeData = {
                    materialRequisitionId: this.id,
                    materialRequisitionDetailId: row.businessId,
                }
                this.$modal.msgSuccess("开始扫码！")
            }
        },
        numberEnterDown() {
            this.keyDownTime = new Date().getTime()
        },
        numberEnterUp() {
            this.keyUpTime = new Date().getTime()
            if (this.keyUpTime - this.keyDownTime < 30) {
                // 扫码枪触发
                let newCode = this.common.codeRule(this.scanCodeData.code)
                // let code = this.scanCodeData.code.split("=")
                // let newCode = code[code.length - 1]
                // console.log(newCode)
                if(newCode.length == 13 || newCode.length == 20){
                    this.scanCodeData.code = newCode
                    if(this.scanCodeType == '0'){
                        // 扫码入库
                        materialRequisitionScanCode(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("扫码成功")
                                morePositionList(this.scanCodeData.materialRequisitionDetailId).then(res => {
                                    if(res.code == 200){
                                        this.moreData = res.rows
                                    }
                                })
                            }else{
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }else{
                        // 删除扫码
                        materialRequisitionScanCodeDel(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("删除成功")
                            }else{
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }
                }else{
                    this.scanCodeData.code = ''
                    this.$refs.getFocus.focus()
                    this.$modal.msgError('扫码错误，请重新扫码!')
                }
            }else{
                // 直接按键盘触发
                let newCode = this.common.codeRule(this.scanCodeData.code)
                // let code = this.scanCodeData.code.split("=")
                // let newCode = code[code.length - 1]
                // console.log(newCode)
                if(newCode.length == 13 || newCode.length == 20){
                    // 扫码入库
                    this.scanCodeData.code = newCode
                    if(this.scanCodeType == '0'){
                        // 扫码入库
                        materialRequisitionScanCode(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("扫码成功")
                                morePositionList(this.scanCodeData.materialRequisitionDetailId).then(res => {
                                    if(res.code == 200){
                                        this.moreData = res.rows
                                    }
                                })
                            }else{
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }else{
                        // 删除扫码
                        materialRequisitionScanCodeDel(this.scanCodeData).then(res => {
                            if(res.code == 200){
                                this.getDetailData()
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgSuccess("删除成功")
                            }else{
                                this.scanCodeData.code = ''
                                this.$refs.getFocus.focus()
                                this.$modal.msgError(res.msg)
                            }
                        })
                    }
                }else{
                    this.scanCodeData.code = ''
                    this.$refs.getFocus.focus()
                    this.$modal.msgError('扫码错误，请重新扫码!')
                }
            }
        },
        // 数量验证
        inputNum(val, row){
            if(row.warehouse !== '' && row.warehouse !== null){
                // 当前选中仓库对应仓库列表的数据
                let isWarehouse = ''
                if(typeof(row.warehouse) == 'string'){
                    isWarehouse = this.warehouseList.find(item => item.name == row.warehouse)
                }else{
                    isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
                }
                // 判断是否启用仓位管理
                if(isWarehouse.openPosition == '0'){
                    if(row.areaId !== '' && row.areaId !== null){
                        morePositionList(row.id).then(res => {
                            if(res.rows.length > 0){
                                row.realNum = ''
                                this.$modal.msgError('已存在多仓位，请去多仓位编辑!')
                            }else{
                                if(val == '' || val == undefined){
                                    this.$modal.msgError('实际数量不能为空!')
                                }else{
                                    if(/^[1-9]\d*$|^0$/.test(val) == false){
                                        this.$modal.msgError('实际数量必须是数字！')
                                        row.realNum = ''
                                        return false
                                    }else{
                                        if(JSON.parse(val) <= 0){
                                            row.realNum = ''
                                            this.$modal.msgError('实际数量必须大于0!')
                                        }else{
                                            if(JSON.parse(val) > row.planNum){
                                                row.realNum = ''
                                                this.$modal.msgError('实际数量不能大于领用数量，请重新输入!')
                                            }
                                        }
                                    }
                                }
                            }
                        })
                    }else{
                        row.realNum = ''
                        this.$modal.msgError('请选择仓位!')
                    }
                }else{
                    if(val == '' || val == undefined){
                        this.$modal.msgError('实际数量不能为空!')
                    }else{
                        if(/^[1-9]\d*$|^0$/.test(val) == false){
                            this.$modal.msgError('实际数量必须是数字！')
                            row.realNum = ''
                            return false
                        }else{
                            if(JSON.parse(val) <= 0){
                                row.realNum = ''
                                this.$modal.msgError('实际数量必须大于0!')
                            }else{
                                if(JSON.parse(val) > row.planNum){
                                    row.realNum = ''
                                    this.$modal.msgError('实际数量不能大于领用数量，请重新输入!')
                                }
                            }
                        }
                    }
                }
            }else{
                row.realNum = ''
                this.$modal.msgError('请选择仓库！')
            }
        },
        // 库存验证
        onInventory(row){
            // console.log(row)
            if(row.realNum !== '' && row.realNum !== null){
                this.getWarehouseConversion()
                if(this.detailData.businessType !== '2'){
                    let obj = {
                        specificationCode: row.productCode,
                        batchNumber: row.batchNumber == null || row.batchNumber == '' ? -1 : row.batchNumber,
                        warehouseId: row.warehouse,
                        areaId: row.areaId,
                        positionId: row.positionId
                    }
                    // 查询库存数量
                    orderSearchStorage(obj).then(res => {
                        if(res.code == 200){
                            if(res.data && res.data > 0){
                                // this.getDetailData()
                                materialRequisitionEditSingle({id: row.id, realNum: row.realNum }).then(res => {
                                    if(res.code == 200){
                                        this.getDetailData()
                                    }
                                })
                            }else{
                                this.$modal.msgError('库存不足！')
                                this.getDetailData()
                                row.realNum = ''
                            }
                        }
                    })
                }
            }
        },
        // 跳转仓库列表页面
        onClickWarehouseList(row, index){
            if(row.warehouse !== '' && row.warehouse !== null){
                // 当前选中仓库对应仓库列表的数据
                let isWarehouse = ''
                if(typeof(row.warehouse) == 'string'){
                    isWarehouse = this.warehouseList.find(item => item.name == row.warehouse)
                }else{
                    isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
                }
                // 判断是否启用仓位管理
                if(isWarehouse.openPosition == '0'){
                    morePositionList(row.id).then(res => {
                        if(res.rows.length > 0){
                            this.$modal.msgError('已存在多仓位，请去多仓位编辑!')
                        }else{
                            this.getWarehouseConversion()
                            if(this.detailData.details[index].warehouse !== null){
                                this.$router.push({path:"/warehouseManagement/warehouse/list", query:{ type: 'materialRequisitionDetail', id: this.id, data: this.detailData, index: index }})
                            }else{
                                this.$modal.msgError('请选择仓库！')
                            }
                        }
                    })
                }else{
                    this.$modal.msgError('未启用仓位管理！')
                }
            }else{
                this.$modal.msgError('请选择仓库！')
            }
        },
        // 多仓位
        showMorePosition(row, item){
            if(item == 'isSerial'){
                morePositionList(row.id).then(res => {
                    if(res.code == 200){
                        this.morePosition = true
                        this.moreData = res.rows
                    }
                })
            }else{
                // 当前选中仓库对应仓库列表的数据
                let isWarehouse = ''
                if(typeof(row.warehouse) == 'string'){
                    isWarehouse = this.warehouseList.find(item => item.name == row.warehouse)
                }else{
                    isWarehouse = this.warehouseList.find(item => item.warehouseId == row.warehouse)
                }
                // 判断是否启用仓位管理
                if(isWarehouse.openPosition == '0'){
                    if(row.areaId !== '' && row.areaId !== null){
                        morePositionList(row.id).then(res => {
                            if(res.code == 200){
                                this.morePosition = true
                                if(res.rows.length > 0 && row.realNum > 0){
                                    res.rows.forEach(item => {
                                        if(item.endNum == 0){
                                            return item.endNum = ''
                                        }
                                    })
                                    this.moreData = res.rows
                                }else{
                                    // 仓库转换
                                    let dataList = this.warehouseList.find(item => {
                                        return item.name == row.warehouse
                                    })
                                    if(dataList != undefined){
                                        row.warehouse = dataList.warehouseId
                                    }
                                    // 多仓位新建
                                    if(res.rows.length == 0 && row.realNum > 0){
                                        let obj = {
                                            businessId: row.id,
                                            businessType: 4,
                                            productId: row.productId,
                                            productCode: row.productCode,
                                            productName: row.productName,
                                            endNum: row.realNum,
                                            warehouse: row.warehouse,
                                            areaId: row.areaId,
                                            areaName: row.areaName,
                                            positionId: row.positionId,
                                            positionName: row.positionName,
                                            batchNumber: row.batchNumber
                                        }
                                        morePositionAdd(obj).then(res => {
                                            if(res.code == 200){
                                                morePositionList(row.id).then(res => {
                                                    this.moreData = res.rows
                                                })
                                            }
                                        })
                                    }else if(res.rows.length >= 0 && row.realNum == ''){
                                        let obj = {
                                            businessId: row.id,
                                            businessType: 4,
                                            productId: row.productId,
                                            productCode: row.productCode,
                                            productName: row.productName,
                                            endNum: '',
                                            warehouse: row.warehouse,
                                            areaId: row.areaId,
                                            areaName: row.areaName,
                                            positionId: row.positionId,
                                            positionName: row.positionName,
                                            batchNumber: row.batchNumber
                                        }
                                        morePositionAdd(obj).then(res => {
                                            if(res.code == 200){
                                                morePositionList(row.id).then(res => {
                                                    res.rows.forEach(item => {
                                                        if(item.endNum == 0){
                                                            return item.endNum = ''
                                                        }
                                                    })
                                                    this.moreData = res.rows
                                                })
                                            }
                                        })
                                    }
                                }
                            }
                        })
                    }else{
                        this.$modal.msgError('请选择仓位！')
                    }
                }else{
                    this.$modal.msgError('未启用仓位管理！')
                }
            }
        },
        // 多仓位跳转仓库列表页面
        morePositionWarehouseList(row, index){
            if(row.endNum > 0){
                this.$modal.msgError('实际数量已存在，不能编辑!')
            }else{
                this.$router.push({path:"/warehouseManagement/warehouse/list", query:{ type: 'materialRequisitionMorePosition', id: this.id, list: row, index: index}})
            }
        },
        // 多仓位数量验证
        inputEditNum(val, row){
            if(row.areaId !== null ){
                if(val == '' || val == undefined){
                    // this.$modal.msgError('实际数量不能为空!')
                }else{
                    if(/^[1-9]\d*$|^0$/.test(val) == false){
                        this.$modal.msgError('实际数量必须是数字！')
                        row.endNum = ''
                        return false
                    }else{
                        if(JSON.parse(val) <= 0){
                            row.endNum = ''
                            this.$modal.msgError('实际数量必须大于0!')
                        }
                    }
                }
            }else{
                row.endNum = ''
                this.$modal.msgError('请选择仓位!')
            }
        },
        // 多仓位数据保存
        onBlurEditNum(val, row){
            if(val.target.value == null || val.target.value == ''){
                this.$modal.msgError('实际数量不能为空!')
            }else{
                let obj = {
                    specificationCode: row.productCode,
                    batchNumber: row.batchNumber == null || row.batchNumber == '' ? -1 : row.batchNumber,
                    warehouseId: row.warehouse,
                    areaId: row.areaId,
                    positionId: row.positionId
                }
                // 查询库存数量
                orderSearchStorage(obj).then(res => {
                    if(res.code == 200){
                        if(res.data && res.data > 0){
                            if(res.data >= row.endNum){
                                morePositionEdit(row).then(res => {
                                    if(res.code == 200){
                                        this.getDetailData()
                                    }
                                })
                            }else{
                                this.$modal.msgError('库存不足！')
                                morePositionList(row.businessId).then(res => {
                                    res.rows.forEach(item => {
                                        if(item.endNum == 0){
                                            return item.endNum = ''
                                        }
                                    })
                                    this.moreData = res.rows
                                })
                            }
                        }else{
                            this.$modal.msgError('库存不足！')
                            morePositionList(row.businessId).then(res => {
                                res.rows.forEach(item => {
                                    if(item.endNum == 0){
                                        return item.endNum = ''
                                    }
                                })
                                this.moreData = res.rows
                            })
                        }
                    }
                })
            }
        },
        // 多仓位单个新建
        onAddMorePosition(row){
            let obj = {
                businessId: row.businessId,
                productId: row.productId,
                productCode: row.productCode,
                productName: row.productName,
                endNum: '',
                warehouse: row.warehouse,
                areaId: '',
                areaName: '',
                positionId: '',
                positionName: '',
                businessType: 4,
                batchNumber: row.batchNumber
            }
            morePositionAddSingle(obj).then(res => {
                if(res.code == 200){
                    morePositionList(row.businessId).then(res => {
                        res.rows.forEach(item => {
                            if(item.endNum == 0){
                                return item.endNum = ''
                            }
                        })
                        this.moreData = res.rows
                    })
                }
            })
        },
        // 多仓位删除一行
        onDeleteMorePosition(row){
            this.$modal.confirm('是否确认删除当前数据？', '删除').then(() => {
                morePositionDelSingle(row.id).then(res => {
                    if(res.code == 200){
                        morePositionList(row.businessId).then(res => {
                            res.rows.forEach(item => {
                                if(item.endNum == 0){
                                    return item.endNum = ''
                                }
                            })
                            this.moreData = res.rows
                            this.getDetailData()
                        })
                    }
                })
            }).catch(() => {})
        },
        // 关闭多仓位
        onHideMorePosition(){
            this.morePosition = false
            // this.getDetailData()
        },
        // 产品仓库转换
        getWarehouseConversion(){
            let arr = this.warehouseList.find(e => {
                return e.name == this.detailData.receivingWarehouse
            })
            if(arr != undefined){
                this.detailData.receivingWarehouse = arr.warehouseId
            }
            this.detailData.details.forEach(i => {
                if(i.warehouse !== null || i.warehouse !== ''){
                    let dataList = this.warehouseList.find(item => {
                        return item.name == i.warehouse
                    })
                    if(dataList != undefined){
                        i.warehouse = dataList.warehouseId
                    }
                }
            })
        },
        // 确定
        confirm(){
            // 判断实际数量不能为空
            // let realNum = this.detailData.details.every(val => !!val.realNum)
            // if(realNum == true){
                this.getWarehouseConversion()
                // console.log(this.detailData)
                confirmPicking(this.detailData).then(res => {
                    if(res.code == 200){
                        this.$modal.msgSuccess("领料成功")
                        this.$tab.closeOpenPage({ path: "/material/materialRequisition/list" })
                    }
                })
            // }else{
            //     this.$modal.msgError('实际数量不能为空！')
            // }
        }
    }
}
</script>

<style scoped>
::v-deep .el-input .el-input-group__append{
  padding: 0 10px;
}
.more{
  font-size: 30px;
  cursor: pointer;
}
::v-deep .el-dialog__header{
  display: none;
}
.icon-more-position{
  font-size: 22px;
  margin: 0 5px;
  cursor: pointer;
}
</style>